﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2020 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using GameFramework.Event;
using GameFramework.Resource;
using UnityEngine;
using UnityGameFramework.Runtime;
using Object = UnityEngine.Object;

namespace StarForce
{
    public abstract class GameBase
    {
        /// <summary>
        /// 游戏模式
        /// </summary>
        public abstract GameMode GameMode
        {
            get;
        }

        protected PlayerHand m_PlayerHand;
        protected UiCardParameters Parameters { get; private set; }

        public bool GameOver
        {
            get;
            protected set;
        }

        private List<Card> m_BaseCardObjectList = new List<Card>();

        public virtual void Initialize()
        {
            GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
            GameEntry.Event.Subscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
            m_PlayerHand = Object.FindObjectOfType<PlayerHand>();
            LoadGlobalConfigAsset("UiCardParameters");
        }
        
        private void LoadGlobalConfigAsset(string configName) {
            string configAssetName = AssetUtility.GetGlobalAsset(configName);
            GameEntry.Resource.LoadAsset(configAssetName, Constant.AssetPriority.FontAsset, new LoadAssetCallbacks(
                (assetName, asset, duration, userData) => {
                    UiCardParameters parameters = asset as UiCardParameters;
                    if (parameters == null) {
                        throw new ArgumentNullException("UiCardParameters canot be cast !");
                    }
                    Parameters = parameters;
                    for (int i=0;i<4;++i) {    
                        int index = 100001 + i;
                        GameEntry.Entity.ShowCard(new CardData(GameEntry.Entity.GenerateSerialId(),100001,index,parameters) {
                            CardSize = new Vector2(30,40),
                            PlayerHand = m_PlayerHand
                        });
                    }
                },
                (assetName, status, errorMessage, userData) =>
                {
                    Log.Error("Can not load asset '{0}' from '{1}' with error message '{2}'.", configName, assetName, errorMessage);
                }));
        }
        
        

        public virtual void Shutdown()
        {
            GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
            GameEntry.Event.Unsubscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
        }

        public virtual void Update(float elapseSeconds, float realElapseSeconds)
        {
            if (Input.GetKeyDown(KeyCode.Tab)) {
                GameEntry.Entity.ShowCard(new CardData(GameEntry.Entity.GenerateSerialId(),100001,100001,Parameters) {
                    CardSize = new Vector2(30,40),
                    PlayerHand = m_PlayerHand
                });
            }
        }

        protected virtual void OnShowEntitySuccess(object sender, GameEventArgs e)
        {
            ShowEntitySuccessEventArgs ne = (ShowEntitySuccessEventArgs)e;
            if (ne.EntityLogicType == typeof(Card))
            {
                var obj = (Card)ne.Entity.Logic;
                m_BaseCardObjectList.Add(obj);
                m_PlayerHand.AddCard(obj);
            }
            
        }

        protected virtual void OnShowEntityFailure(object sender, GameEventArgs e)
        {
            ShowEntityFailureEventArgs ne = (ShowEntityFailureEventArgs)e;
            Log.Warning("Show entity failure with error message '{0}'.", ne.ErrorMessage);
        }
    }
}
